package com.suxin.call_service_hello_ribbion.remoteService;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {


    private Logger logger = LoggerFactory.getLogger(HelloService.class);

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "helloError")
    public String hello(String name) {
        long time = System.currentTimeMillis();
        String result = restTemplate.getForObject("http://service-hello/hello?name=" + name, String.class);

        logger.info("远程调用service_hello,耗时:{}ms",System.currentTimeMillis() - time);
        return result;
    }

    //发生远程调用失败，则执行此方法
    private String helloError(String name) {
        logger.error("远程调用service_hello失败");
        return "i am sorry "+name+", call service_hello happens error";
    }
}
